<?php

namespace app\index\controller;

use think\Controller;
use app\validate\User;
use think\Validate;

class ValidateDemo extends Controller{

	//验证器
	function index(){
		//要验证的数据
		$data = [
				'name'=>'SunRock',
				'email'=>'sunrock@qq.com',
				'password'=>'123abc',
				'mobile'=>'13212312312',
		];
		
		//实例化验证类
		$validate = new User();
		$res = $validate->check($data);
		$error = $validate->getError();

		//套静态马甲
// 		$res = \app\facade\User::check($data);
// 		$error = \app\facade\User::getError();
		
		if($res){
			return '验证通过';
		}else{
			return $error;
		}
	}
	
	/**
	 * 调用控制器中的validate方法进行验证：使用用户自定义的验证类
	 */
	function index2(){
		//要验证的数据
		$data = [
// 				'name'=>'SunRock',
				'email'=>'sunrock@qq.com',
				'password'=>'123abc',
				'mobile'=>'13212312312',
		];
		
		$validate = 'app\validate\User';
		$message = ['name'=>'名字错啦'];
		
		$res = $this->validate($data, $validate, $message);
		
		dump($res);
	}
	
	/**
	 * 独立验证
	 */
	function index3(){
		$rule_name = [
				'name|姓名'=>[
						'require',		//必填
						'min'=>5,		//最短
						'max'=>20,		//最长
				],
				'email'=>[
						'require',
						'email',
				],
				'password'=>[
						'require',
						'min'=>6,
						'max'=>12,
						'alphaNum'		//只能使用字母（alpha）和数字（Num)
				],
				'mobile'=>[
						'require',
						'mobile',
				],
		];
		
		$validate = new Validate();
		$validate->rule($rule_name);
		$validate->message(['name'=>'名字不能为空啊啊啊啊啊啊啊']);
		
		$data = [
				'name'=>'SunRock',
				'email'=>'sunrock@qq.com',
				'password'=>'123abc',
				'mobile'=>'13212312312',
		];
		$res = $validate->check($data);
		$error = $validate->getError();
		
		if($res){
			return '验证通过';
		}else{
			return $error;
		}
	}
}